package com.lijin.batchsys.framework.control;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;

import com.lijin.batchsys.framework.common.BatchUtil;
import com.lijin.batchsys.framework.common.IBatchLogger;
import com.lijin.batchsys.framework.dao.model.BatchExecLog;
import com.lijin.batchsys.framework.dao.model.BatchExecLogDetail;
import com.lijin.batchsys.framework.dao.model.BatchInfo;
import com.lijin.batchsys.framework.dao.model.JobInfo;
import com.lijin.batchsys.framework.enums.RunningMode;
import com.lijin.batchsys.framework.enums.RunningStatus;

public class BatchExecContext {
	
	private static final Logger logger = Logger.getLogger(BatchExecContext.class);
	
	private RunningMode runningMode = null; // 批次执行模式
	private BatchInfo runningBatchInfo; // 执行的批次信息
	private String batchDateStr; // 批次日期
	private RunningStatus status;
	private String execMsg;
	private List<JobInfo> errJobLst = new ArrayList<JobInfo>();
	private BatchExecLog batchExecLog;
	private IBatchLogger batchLogger;

	public BatchExecContext(RunningMode runningMode, BatchInfo runningBatchInfo, Date batchDate) {
		this.runningMode = runningMode;
		this.runningBatchInfo = runningBatchInfo;
		this.batchDateStr = BatchUtil.formatDateTo8Str(batchDate);
		this.status = RunningStatus.RUNNING;
		this.execMsg = "";
	}
	
	/**
	 * 根据批次号、任务号和步骤号获取批次日志详情
	 * @param batchNo
	 * @param jobNo
	 * @param stepNo
	 * @return
	 */
	public BatchExecLogDetail getBatchLogDetail(int batchNo, int jobNo, int stepNo) {
		for (BatchExecLogDetail detail : batchExecLog.getBatchExecLogDetails()) {
			if (batchNo == detail.getBatchNo()
					&& jobNo == detail.getJobNo()
					&& stepNo == detail.getStepNo()) {
				return detail;
			}
		}
		return null;
	}
	
	/**
	 * 记录上下文状态
	 */
	public void logContext() {
		if (batchLogger != null) {
			// 记录上下文状态的操作如果失败，则打印失败信息，但是不中断当前批量处理流程
			try {
				if (batchExecLog == null) {
					batchLogger.logBatchContext(this, false);
				} else {
					batchLogger.logBatchContext(this, true);
				}
			} catch (Exception e) {
				logger.error("记录上下文状态出错，批次信息:" + this.getRunningBatchInfo().getSummary(), e);
			}
		}
	}
	
	public void addErrJob(JobInfo errJob) {
		errJobLst.add(errJob);
	}
	
	public BatchInfo getRunningBatchInfo() {
		return runningBatchInfo;
	}

	public void setRunningBatchInfo(BatchInfo runningBatchInfo) {
		this.runningBatchInfo = runningBatchInfo;
	}

	public String getBatchDateStr() {
		return batchDateStr;
	}

	public void setBatchDateStr(String batchDateStr) {
		this.batchDateStr = batchDateStr;
	}

	public BatchExecLog getBatchExecLog() {
		return batchExecLog;
	}

	public void setBatchExecLog(BatchExecLog batchExecLog) {
		this.batchExecLog = batchExecLog;
	}

	public RunningMode getRunningMode() {
		return runningMode;
	}

	public void setRunningMode(RunningMode runningMode) {
		this.runningMode = runningMode;
	}

	public RunningStatus getStatus() {
		return status;
	}

	public void setStatus(RunningStatus status) {
		this.status = status;
	}

	public void setBatchLogger(IBatchLogger batchLogger) {
		this.batchLogger = batchLogger;
	}

	public String getExecMsg() {
		return execMsg;
	}
	
	public void setExecMsg(String execMsg) {
		this.execMsg = execMsg;
	}
}
